package com.hongxun.design.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.adapters.ProjectStatisticSearchAdapter;
import com.hongxun.design.bean.SearchBean;
import com.hongxun.design.bean.UnitBean;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.utils.DataUtil;

import java.util.List;


/**
 * Created by zerdoor_pc on 2016/6/12.
 * 项目统计条件
 */
public class StatisticsSearchActivity extends BaseActivity implements HeadSecond {


    ListView search_list;
    static List<SearchBean> data;
    private Handler mHandler;
    public final int UPDATE = 1;
    public final int SEARCH = 2;
    public static ProjectStatisticSearchAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statisticsearch);
        findBar();
        findView();
        initialization();
    }

    @Override
    public void findBar() {
        HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.statisticsearch_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean(IconSourse.screen + "", View.GONE, R.color.bar_right, ListenerSet.back, "设置统计条件", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));

    }

    @Override
    protected void onResume() {
        MyApp.activity = this;
        mActivity = this;
        baseActivity = this;
        super.onResume();
    }

    @Override
    protected void findView() {
        super.findView();
        search_list = (ListView) findViewById(R.id.statisticsearch_list);
    }

    @Override
    protected void initialization() {
        super.initialization();
        addHandler();
        adapter=null;
        if (data == null) data = DataUtil.getProjectSearchBeanList();
         adapter = new ProjectStatisticSearchAdapter(this, data, mHandler);
        search_list.setAdapter(adapter);

    }

    @Override
    protected void onDestroy() {
        adapter=null;
        super.onDestroy();
    }

    /**
     * 事件处理*
     */
    private void addHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case UPDATE:
                        StatisticalAnalysisActivity.startYear = "";
                        StatisticalAnalysisActivity.endYear = "";
                        StatisticalAnalysisActivity.design_unit = "";
                        StatisticalAnalysisActivity.owner_unit = "";
//                        data=null;
//                        adapter=null;
//                        StatisticsSearchActivity.this.onBackPressed();
                        if (data == null) {
                            data = DataUtil.getProjectSearchBeanList();
                        } else {
                            data.clear();
                            data.addAll(DataUtil.getProjectSearchBeanList());
                        }
                       if (adapter!=null)adapter.notifyDataSetChanged();
                      //  mHandler.sendEmptyMessage(UPDATE);
                        break;
                    case SEARCH:
                        //fragment.project_name = search_input.getText().toString();
                        for (SearchBean s : data) {
                            if (s.getValueCode().toString().length() > 0) {
                                switch (s.getNameCode()) {
                                    case MString.year:
                                        String[] tar = s.getValueCode().toString().split("—");
                                        try {
                                            StatisticalAnalysisActivity.startYear = tar[0];
                                            StatisticalAnalysisActivity.endYear = tar[1];
                                        } catch (Exception e) {
                                        }
                                        break;
                                    case MString.current_stage:
                                        //fragment.current_stage = (String) s.getValueCode();
                                        break;
                                    case MString.project_type:
                                        // fragment.project_type = (String) s.getValueCode();
                                        break;
                                    case MString.current_schedule:
                                        //fragment.current_schedule = (String) s.getValueCode();
                                        break;
                                    case MString.owner_unit:
                                        UnitBean bean = (UnitBean) s.getValueCode();
                                        StatisticalAnalysisActivity.owner_unit = bean.getId();
                                        break;
                                    case MString.design_unit:
                                        UnitBean bean2 = (UnitBean) s.getValueCode();
                                        MyLog.L(MString.SUCCESSED + " UnitBean=" + JSON.toJSONString(bean2));
                                        StatisticalAnalysisActivity.design_unit = bean2.getId();
                                        break;
                                }

                            }

                        }
                        // fragment.onRefresh();
                        adapter=null;
                        StatisticsSearchActivity.this.onBackPressed();
                        break;

                }
            }
        };
    }

}
